package com.ttg.web.controller.common;

import com.ttg.common.entities.BasePage;
import com.ttg.common.entities.BaseResult;
import com.ttg.common.utils.RequestParamsManager;
import com.ttg.model.dto.*;
import com.ttg.model.vo.*;
import com.ttg.web.service.QualityManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Description： 质量管理controller类
 * Author: chenyou
 * Date: 2025/8/18 15:12
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Slf4j
@Api("质量管理")
@RestController
@RequestMapping("/quality/manage")
public class QualityManageController {

    @Autowired
    private QualityManageService qualityManageService;

    @ApiOperation("行业/商户质量规则列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度")
    })
    @RequestMapping("/rulelist")
    public BaseResult rulelist(@RequestBody(required = false) String data){
        QualityManageRuleListDTO reqDto = RequestParamsManager.transformRequestParam(data, QualityManageRuleListDTO.class);
        return BaseResult.success(qualityManageService.rulelist(reqDto));
    }

    @ApiOperation("行业/商户质量维度权重配置-预览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wcDimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度")
    })
    @RequestMapping("/weightConfig/show")
    public BaseResult<QMWeightConfigVO> weightConfigShow(@RequestBody(required = false) String data){
        QMWeightConfigShowDTO reqDto = RequestParamsManager.transformRequestParam(data, QMWeightConfigShowDTO.class);
        return BaseResult.success(qualityManageService.weightConfigShow(reqDto));
    }

    @ApiOperation("行业/商户质量维度权重配置-编辑保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wcDimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "wcLivenessWeight",value = "活跃度阶梯评价权重,单位：%",required = true,dataType = "bigDecimal",paramType = "form"),
            @ApiImplicitParam(name = "wcClearCostWeight",value = "清算成本阶梯评价权重,单位：%",required = true,dataType = "bigDecimal",paramType = "form"),
            @ApiImplicitParam(name = "wcAvgWeight",value = "户均归集阶梯评价权重,单位：%",required = true,dataType = "bigDecimal",paramType = "form"),

    })
    @RequestMapping("/weightConfig/editSave")
    public BaseResult weightConfigEditSave(@RequestBody(required = false) String data){
        QMWeightConfigSaveDTO reqDto = RequestParamsManager.transformRequestParam(data, QMWeightConfigSaveDTO.class);
        qualityManageService.weightConfigEditSave(reqDto);
        return BaseResult.success("ok");
    }

    @ApiOperation("行业/商户阶梯评价规则-预览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ruleDimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "ruleType",value = "阶梯规则类型",required = true,dataType = "integer",paramType = "form",example = "0-行业活跃度，1-清算成本,2-户均归集,3-商户活跃天数")
    })
    @RequestMapping("/ladderRule/show")
    public BaseResult<QMLadderRuleVO> ladderRuleShow(@RequestBody(required = false) String data){
        QMLadderRuleShowDTO reqDto = RequestParamsManager.transformRequestParam(data, QMLadderRuleShowDTO.class);
        return BaseResult.success(qualityManageService.ladderRuleShow(reqDto));
    }

    @ApiOperation("行业/商户阶梯评价规则-编辑保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ruleDimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "ruleType",value = "阶梯规则类型",required = true,dataType = "integer",paramType = "form",example = "0-行业活跃度，1-清算成本,2-户均归集,3-商户活跃天数"),
            @ApiImplicitParam(name = "list",value = "阶梯规则列表,list集合",required = true,dataType = "list",paramType = "form")
    })
    @RequestMapping("/ladderRule/editSave")
    public BaseResult ladderRuleEditSave(@RequestBody(required = false) String data){
        QMLadderRuleSaveDTO reqDto = RequestParamsManager.transformRequestParam(data, QMLadderRuleSaveDTO.class);
        qualityManageService.ladderRuleEditSave(reqDto);
        return BaseResult.success("ok");
    }

    @ApiOperation("行业/商户质量分类规则-条件符号枚举值")
    @RequestMapping("/typeRule/symbol")
    public BaseResult<List<SymbolVO>> symbol(){
        return BaseResult.success(qualityManageService.symbol());
    }

    @ApiOperation("行业/商户质量分类规则-预览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度")
    })
    @RequestMapping("/typeRule/show")
    public BaseResult<List<QMTypeRuleVO>> typeRuleShow(@RequestBody(required = false) String data){
        QMTypeRuleShowDTO reqDto = RequestParamsManager.transformRequestParam(data, QMTypeRuleShowDTO.class);
        return BaseResult.success(qualityManageService.typeRuleShow(reqDto));
    }

    @ApiOperation("行业/商户质量分类规则-编辑保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "list",value = "质量管理分类规则,list集合",required = true,dataType = "list",paramType = "form")
    })
    @RequestMapping("/typeRule/editSave")
    public BaseResult typeRuleEditSave(@RequestBody(required = false) String data){
        QMTypeRuleSaveDTO reqDto = RequestParamsManager.transformRequestParam(data, QMTypeRuleSaveDTO.class);
        qualityManageService.typeRuleEditSave(reqDto);
        return BaseResult.success("ok");
    }

    @ApiOperation("质量报告-可选年份列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "year",value = "报告所属年",required = true,dataType = "string",paramType = "form")
    })
    @RequestMapping("/reportSelectYears")
    public BaseResult reportSelectYears(@RequestBody(required = false) String data){
        QMReportYearMonthDTO reqDto = RequestParamsManager.transformRequestParam(data, QMReportYearMonthDTO.class);
        return BaseResult.success(qualityManageService.reportSelectYears(reqDto));
    }

    @ApiOperation("质量报告-可选月份列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "year",value = "报告所属年",required = true,dataType = "string",paramType = "form")
    })
    @RequestMapping("/reportSelectMonths")
    public BaseResult reportSelectMonths(@RequestBody(required = false) String data){
        QMReportYearMonthDTO reqDto = RequestParamsManager.transformRequestParam(data, QMReportYearMonthDTO.class);
        return BaseResult.success(qualityManageService.reportSelectMonths(reqDto));
    }

    @ApiOperation("行业质量报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "year",value = "报告所属年",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "month",value = "报告所属月",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "clsId1",value = "行业一级分类",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "clsId2",value = "行业二级分类",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "totalMctCountLowerBound",value = "总商户数下限值",required = true,dataType = "integer",paramType = "form"),
            @ApiImplicitParam(name = "activeMctCountLowerBound",value = "活跃商户数下限值",required = true,dataType = "integer",paramType = "form"),
            @ApiImplicitParam(name = "clearCostLowerBound",value = "清算成本下限值",required = true,dataType = "BigDecimal",paramType = "form"),
            @ApiImplicitParam(name = "activeDayLowerBound",value = "活跃天数下限值",required = true,dataType = "integer",paramType = "form"),
    })
    @RequestMapping("/industryReport")
    public BaseResult industryReport(@RequestBody(required = false) String data){
        QMReportDTO reqDto = RequestParamsManager.transformRequestParam(data, QMReportDTO.class);
        return BaseResult.success(qualityManageService.industryReport(reqDto));
    }

    @ApiOperation("行业质量报告下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "year",value = "报告所属年",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "month",value = "报告所属月",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "clsId1",value = "行业一级分类",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "clsId2",value = "行业二级分类",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "totalMctCountLowerBound",value = "总商户数下限值",required = true,dataType = "integer",paramType = "form"),
            @ApiImplicitParam(name = "activeMctCountLowerBound",value = "活跃商户数下限值",required = true,dataType = "integer",paramType = "form"),
            @ApiImplicitParam(name = "clearCostLowerBound",value = "清算成本下限值",required = true,dataType = "BigDecimal",paramType = "form"),
            @ApiImplicitParam(name = "activeDayLowerBound",value = "活跃天数下限值",required = true,dataType = "integer",paramType = "form"),
    })
    @RequestMapping("/industryReportDownload")
    public void industryReportDownload(@RequestBody(required = false) String data, HttpServletResponse response){
        QMReportDTO reqDto = RequestParamsManager.transformRequestParam(data, QMReportDTO.class);
//        reqDto.setDimType(0);
//        reqDto.setYear("2025");
//        reqDto.setMonth("06");
//        reqDto.setClsId1(1);
//        reqDto.setClsId2(11);
        qualityManageService.industryReportDownload(reqDto,response);
    }

    @ApiOperation("商户质量报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "year",value = "报告所属年",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "month",value = "报告所属月",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "clsId1",value = "行业一级分类",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "clsId2",value = "行业二级分类",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "totalMctCountLowerBound",value = "总商户数下限值",required = true,dataType = "integer",paramType = "form"),
            @ApiImplicitParam(name = "activeMctCountLowerBound",value = "活跃商户数下限值",required = true,dataType = "integer",paramType = "form"),
            @ApiImplicitParam(name = "clearCostLowerBound",value = "清算成本下限值",required = true,dataType = "BigDecimal",paramType = "form"),
            @ApiImplicitParam(name = "activeDayLowerBound",value = "活跃天数下限值",required = true,dataType = "integer",paramType = "form"),
    })
    @RequestMapping("/merchReport")
    public BaseResult merchReport(@RequestBody(required = false) String data){
        QMReportDTO reqDto = RequestParamsManager.transformRequestParam(data, QMReportDTO.class);
        return BaseResult.success(qualityManageService.merchReport(reqDto));
    }

    @ApiOperation("商户质量报告下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dimType",value = "维度类型",required = true,dataType = "integer",paramType = "form",example = "0-商户维度，1-行业维度"),
            @ApiImplicitParam(name = "year",value = "报告所属年",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "month",value = "报告所属月",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "clsId1",value = "行业一级分类",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "clsId2",value = "行业二级分类",required = true,dataType = "string",paramType = "form"),
            @ApiImplicitParam(name = "totalMctCountLowerBound",value = "总商户数下限值",required = true,dataType = "integer",paramType = "form"),
            @ApiImplicitParam(name = "activeMctCountLowerBound",value = "活跃商户数下限值",required = true,dataType = "integer",paramType = "form"),
            @ApiImplicitParam(name = "clearCostLowerBound",value = "清算成本下限值",required = true,dataType = "BigDecimal",paramType = "form"),
            @ApiImplicitParam(name = "activeDayLowerBound",value = "活跃天数下限值",required = true,dataType = "integer",paramType = "form"),
    })
    @RequestMapping("/merchReportDownload")
    public void merchReportDownload(@RequestBody(required = false) String data, HttpServletResponse response){
        QMReportDTO reqDto = RequestParamsManager.transformRequestParam(data, QMReportDTO.class);
//        reqDto.setDimType(0);
//        reqDto.setYear("2025");
//        reqDto.setMonth("06");
        qualityManageService.merchReportDownload(reqDto,response);
    }

}
